package game 
{
	
	import net.flashpunk.FP;
	import game.Player;
	
	/**
	 * ...
	 * @author Matt Hofmann
	 */
	public class Camera 
	{
		// Camera offset - how close the player needs to be to the edge of the screen,
		// before camera panning kicks in
		private var _cameraOffset:int = 160;
		private var mapWidth:int;
		private var mapHeight:int;
		
		public function Camera() 
		{
			
		}
		
		public function setMapSize(_mapWidth:int, _mapHeight:int):void
		{
			this.mapWidth = _mapWidth;
			this.mapHeight = _mapHeight;
		}
		
		/** 
		 * Called on init - adjusts the current FP.camera coordinates, so that player is centered
		 * */
		public function adjustToPlayer(player:Player):void
		{
			// Find the coordinates to that would center the player 
			var newCameraX:int = (player.x + player.width/2) - FP.width / 2;
			var newCameraY:int = (player.y + player.height/2) - FP.height / 2;
			
			// Check if they go beyond map boundaries
			if (newCameraX < 0) newCameraX = 0;
			else if (newCameraX + FP.width > mapWidth) newCameraX = mapWidth - FP.width;
			
			if (newCameraY < 0) newCameraY = 0;
			else if (newCameraY + FP.height > mapHeight) newCameraY = mapHeight - FP.height;
			
			// Set the camera coordinates
			FP.camera.x = newCameraX;
			FP.camera.y = newCameraY;
		}
		
		/**
		 * Called from World update - pans the camera if necessary, to "follow" the player around
		 * Only do this if the player is within _cameraOffset from an edge of the screen,
		 * and if player is actually moving towards that edge
		 * @param	mapWidth   - width of map, from mapGrid
		 * @param	mapHeight  - height of map, from mapGrid
		 * @param	player     - Player entity
		 */
		public function followPlayer(player:Player):void
		{
			// Check horizontal axis
			// Check if player is moving LEFT, and if player is close to the left edge
			if (player.getXSpeed() < 0 && player.x - FP.camera.x < _cameraOffset) 
			{
				// Only if the screen's left edge 
				// didn't hit the left most boundary x=0
				if (FP.camera.x > 0) FP.camera.x += player.getXSpeed();
			}
			// Check if player is moving RIGHT, and if player is close to the right edge
			else if (player.getXSpeed() > 0 && (FP.camera.x + FP.width) -  (player.x + player.width) < _cameraOffset)
			{
				// Only if the screen's right edge 
				// didn't hit the right most boundary x=mapWidth
				if (FP.camera.x + FP.width < mapWidth) FP.camera.x += player.getXSpeed();
			}
			
			// Check vertical axis
			// Check if player is moving UP, and if player is close to the top
			if (player.getYSpeed() < 0 && player.y - FP.camera.y < _cameraOffset) 
			{
				// Only if the screen's upper edge 
				// didn't hit the up most boundary y=0
				if (FP.camera.y > 0) FP.camera.y += player.getYSpeed();
			}
			// Check if player is moving DOWN, and if player is close to the bottom
			else if (player.getYSpeed() > 0 && (FP.camera.y + FP.height) - (player.y + player.height) < _cameraOffset)
			{
				// Only if the screen's bottom edge 
				// didn't hit the bottom most boundary x=mapHeight
				if (FP.camera.y + FP.height < mapHeight) FP.camera.y += player.getYSpeed();
			}
		}
	}
}